package com.drawu.drawuspring.controller;


import com.drawu.drawuspring.domain.DTO.*;
import com.drawu.drawuspring.domain.Result;
import com.drawu.drawuspring.domain.VO.OBS;
import com.drawu.drawuspring.domain.VO.PostVO;
import com.drawu.drawuspring.service.IForumService;
import com.obs.services.exception.ObsException;
import com.obs.services.model.PutObjectRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/forum")
@RequiredArgsConstructor
@Tag(name = "帖子模块", description = "forum api")
public class ForumController {


    private static final Logger log = LoggerFactory.getLogger(ForumController.class);
    private final IForumService forumService;

    // 获取所有帖子
    @Operation(summary = "获取所有帖子,    area参数可选，为空时返回所有帖子,不为空时返回分区为“官方公告”的帖子")
    @GetMapping("/page")
    public Result<PageDTO<PostVO>> getAllPost(PostPageDto postPageDto) {
        return Result.success(forumService.getAllPosts(postPageDto));
    }

    // 获取所有帖子
    @Operation(summary = "ai辅助写动态")
    @PostMapping("/chat")
    public Result<PostDTO> chat(@RequestBody PostDTO postDTO) throws Exception {
        return Result.success(forumService.chat(postDTO));
    }

    //     根据id查询帖子内容
    @Operation(summary = "根据id查询帖子内容")
    @GetMapping("/postDetail")
    public Result<PostVO> getPostDetail(@RequestParam Integer id) {
        return Result.success(forumService.getPostDetail(id));
    }


    // 查询用户收藏的所有帖子
    @Operation(summary = "查询用户收藏的所有帖子")
    @GetMapping("/getAllCollect")
    public Result<List<PostVO>> getAllCollect() {
        return Result.success(forumService.getAllCollect());
    }
    /**
     * 创建新帖子
     */
    @Operation(summary = "创建新帖子")
    @PostMapping("/createPost")
    public Result createPost(@RequestBody PostDTO dto) throws Exception {
        return Result.success(forumService.createPost(dto));
    }



    // 回复帖子

    // 上传图片
    @Operation(summary = "上传帖子图片")
    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam("file") MultipartFile mpFile) throws IOException {
        return forumService.uploadImage(mpFile);
    }

    // 删除图片
    @Operation(summary = "删除图片")
    @DeleteMapping("/deleteImage")
    public Result deleteImage(@RequestBody DeleteImageDto deleteImageDto) {
        return forumService.deleteImage(deleteImageDto);
    }

    // 测试
    @Operation(summary = "测试")
    @PostMapping("/test")
    public String test(@RequestParam("file") MultipartFile file, @RequestParam("user") String user) {
        try {
            PutObjectRequest request = new PutObjectRequest();

            // 创建一个临时文件
            // 获取后缀
            String extension = file.getOriginalFilename();
            int lastIndexOf = Objects.requireNonNull(extension).lastIndexOf(".");
            if (lastIndexOf == -1) {
                extension = ""; // empty extension
            }
            extension = extension.substring(lastIndexOf);
            File convFile = File.createTempFile(user, extension);
            // 将MultipartFile的内容保存到临时文件
            file.transferTo(convFile);
            System.out.println(convFile.getName());

            request.setFile(convFile);

            request.setBucketName("drawu");
            request.setObjectKey("images/" + convFile.getName());
            OBS.client.putObject(request);
            System.out.println("putObject successfully");
        } catch (ObsException e) {
            System.out.println("putObject failed");
            // 请求失败,打印http状态码
            System.out.println("HTTP Code:" + e.getResponseCode());
            // 请求失败,打印服务端错误码
            System.out.println("Error Code:" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            System.out.println("Error Message:" + e.getErrorMessage());
            // 请求失败,打印请求id
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
        } catch (Exception e) {
            System.out.println("putObject failed");
            // 其他异常信息打印
            e.printStackTrace();
        }
        return "Hello World!";
    }

    /**
     * 用户点赞或取消点赞·接口
     */
    @Operation(summary = "用户点赞或取消点赞·接口")
    @PostMapping("/like")
    public Result like(@RequestBody LikesBehaviorDto dto) {
        return forumService.like(dto);
    }

    /**
     * 用户点赞或取消点赞·接口
     */
    @Operation(summary = "用户收藏或取消收藏·接口")
    @PostMapping("/collect")
    public Result collect(@RequestBody CollectBehaviorDto dto) {
        return forumService.collect(dto);
    }

    /**
     * 获取token
     *
     */
    @Operation(summary = "获取token")
    @PostMapping("/getToken")
    public String getToken() {
      return forumService.getToken().toString();
    }

    /**
     * 根据帖子id删除帖子
     */
    @Operation(summary = "根据帖子id删除帖子")
    @DeleteMapping("/deletePost")
    public Result deletePost(@RequestParam("id") Integer id) {
        return forumService.deletePost(id);
    }

    /**
     * 根据帖子id编辑帖子
     */
    @Operation(summary = "根据帖子id编辑帖子")
    @PostMapping("/editPost")
    public Result editPost(@RequestBody PostDTO dto) {
        return forumService.editPost(dto);
    }
}
